(function(angular){
    //创建模块
    var app = angular.module('movie_list',['ngRoute','jsonpService']);
    //配置路由信息
    app.config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/:moveType/:page?',{
            templateUrl:'./movie_list/movie_list.html',
            controller:"movie_listController"
        })
    }]);
    app.controller('movie_listController',['$scope',
        '$http',//专门用来发送请求的
        '$route',
        '$routeParams',
        'myService',
        function ($scope,$http,$route,$routeParams,myService) {
           //顯示動畫信息
            $scope.loading =true;
            $scope.pageSize =5;//每页的条数
            $scope.page = ($routeParams.page|| '1') -0;//第几页
            var start = ($scope.page-1)*$scope.pageSize;

            //使用自己封装的jsonp,进行同源数据的访问
            myService.jsonp('https://api.douban.com/v2/movie/'+$routeParams.moveType,{
                start:start,count:$scope.pageSize,q:$routeParams.q
            },function(data){
                $scope.data=data;
                //根据总条数和每页大小计算出总页数
                $scope.totalPage =Math.ceil($scope.data.total/$scope.pageSize);
                $scope.loading =false;
                $scope.$apply();
            })
            $scope.getPage = function (nowPage) {
                if(nowPage<=0 || nowPage>$scope.totalPage){
                return nowPage;
                }
                $route.updateParams({page:nowPage});
            }
    }]);
})(angular)
